package com.macllvm.whale.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.content.ComponentName;
import android.content.Context;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.util.Log;

import java.lang.Override;
import java.util.ArrayList;
import java.util.List;
import com.macllvm.whale.protocol.ProtocolController;
import com.macllvm.whale.service.NetworkService;
import com.macllvm.whale.R;

public class MainActivity extends FragmentActivity  {
    private static final String TAG = "MainActivity";
    private ViewPager mViewPager;
    private FragmentPagerAdapter mAdapter;
    private List<Fragment> mFragments = new ArrayList<Fragment>();

    private RadioButton mTabBuddys;
    private RadioButton mTabTransfers;
    private RadioButton mTabSettings;
    private RadioGroup  mTabGroup;
    private TextView  mTopBarTitle;
    private String[]  mTitles;
    private ProtocolController mController;


    @Override
    protected void onCreate(Bundle savedInstanceState)   {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_main);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);

        init();
        connectNetworkService();
    }


    @Override
    protected void onResume() {
        super.onResume();
        Log.v(TAG, "AminActivity onResume........");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.v(TAG, "AminActivity onStop........");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.v(TAG, "AminActivity onPause........");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        disconnectNetworkService();
    }

    @Override
    public void onBackPressed() {
        Intent intent= new Intent(Intent.ACTION_MAIN);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addCategory(Intent.CATEGORY_HOME);
        startActivity(intent);
    }

    private void connectNetworkService() {
        Intent intent = new Intent(MainActivity.this, NetworkService.class);
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    }

    private void disconnectNetworkService() {
        if(null != serviceConnection )
            unbindService(serviceConnection);
    }

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    private void init()    {
        mViewPager = (ViewPager)findViewById(R.id.id_viewpager);
        mTopBarTitle = (TextView)findViewById(R.id.topbar_title);
        mTabBuddys = (RadioButton)findViewById(R.id.tab_btn_buddys);
        mTabTransfers = (RadioButton)findViewById(R.id.tab_btn_transfers);
        mTabSettings = (RadioButton)findViewById(R.id.tab_btn_settings);
        mTabGroup = (RadioGroup)findViewById(R.id.tab_group);

        mTitles = new String[] { this.getString(R.string.tab_buddys),
                                 this.getString(R.string.tab_transfers),
                                 this.getString(R.string.tab_settings) };

        mTopBarTitle.setText(mTitles[0]);
        mFragments.add(new BuddysFragment());
        mFragments.add(new TransfersFragment());
        mFragments.add(new SettingsFragment());

        mTabGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
            int radioButtonId = group.getCheckedRadioButtonId();
            switch(checkedId) {
                case R.id.tab_btn_buddys:
                    mViewPager.setCurrentItem(0, true);
                    break;
                case R.id.tab_btn_transfers:
                    mViewPager.setCurrentItem(1, true);
                    break;
                case R.id.tab_btn_settings:
                    mViewPager.setCurrentItem(2, true);
                    break;
            }
            }
        });


        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager())  {
            @Override
            public int getCount() {
                return mFragments.size();
            }

            @Override
            public Fragment getItem(int arg0)  {
                return mFragments.get(arg0);
            }
        };

        mViewPager.setAdapter(mAdapter);
        mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
            //private int currentIndex = 0;
            @Override
            public void onPageSelected(int pos) {
                //if(currentIndex == pos) return;
                switch (pos) {
                    case 0:
                        mTabBuddys.setChecked(true);
                        break;
                    case 1:
                        mTabTransfers.setChecked(true);
                        break;
                    case 2:
                        mTabSettings.setChecked(true);
                        break;
                }
                mTopBarTitle.setText(mTitles[pos]);
                //currentIndex = pos;
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
    }
}
